#!/bin/bash

scriptname=`readlink -e "$0"`
scriptpath=`dirname "$scriptname"`
datetime=`date +%Y%m%d_%H%M`
set -e
$scriptpath/fixpermissions.sh

. /etc/lsb-release
ARCH=`arch`
ver=${DISTRIB_ID}_${DISTRIB_RELEASE}_${ARCH}
hostname=`cat /etc/hostname`

cd $scriptpath/

if [[ ! -f install/etc/opt/mythtv/${hostname}_mythtv.conf ]] ; then
    echo "Please set up install/etc/opt/mythtv/${hostname}_mythtv.conf"
    exit 2
fi
. install/etc/opt/mythtv/${hostname}_mythtv.conf

create_dir() {
    perm=$2
    if [[ "$perm" == "" ]] ; then
        perm=775
    fi
    sudo mkdir -pv "$1"
    sudo chown mythtv "$1"
    sudo chgrp mythtv "$1"
    sudo chmod $perm "$1"
}

$scriptpath/fixpermissions.sh

if [[ "$IS_BACKEND" == true ]] ; then
    create_dir $VIDEODIR 2775
fi
create_dir $DATADIR 2775
create_dir /opt/mythtv
create_dir /opt/mythtv/bin
create_dir /etc/opt/mythtv
create_dir /etc/rc_keymaps
# create_dir $MOUNTDIR
create_dir $LOGDIR 2775
if [[ "$IS_BACKEND" == true ]] ; then
    create_dir $VIDEODIR/video1 2775
    create_dir $VIDEODIR/video2 2775
    create_dir $VIDEODIR/video3 2775
    create_dir $VIDEODIR/video4 2775
fi

if [[ `echo "$TCMOUNTDIR"/*` != "$TCMOUNTDIR/*" ]] ; then
    create_dir $TCMOUNTDIR/keepalive 2775
fi

mkdir -p $scriptpath/backup/
if [[ -f /etc/opt/mythtv/mythtv.conf ]] ; then
    if ! diff install/etc/opt/mythtv/${hostname}_mythtv.conf /etc/opt/mythtv/mythtv.conf ; then
        cp -p /etc/opt/mythtv/mythtv.conf $scriptpath/backup/${hostname}_${datetime}_mythtv.conf
    fi
fi
if [[ "$IS_BACKEND" == true ]] ; then
    if ! diff install/etc/init/mythtv-backend.conf /etc/init/mythtv-backend.conf ; then
        cp -p /etc/init/mythtv-backend.conf $scriptpath/backup/${hostname}_${datetime}_mythtv-backend.conf
    fi
fi
pushd install/etc/opt/mythtv/
# Remove old options files
rm -f /etc/opt/mythtv/*.options
for file in ${hostname}_* ; do
    cp -pv "$file" /etc/opt/mythtv/${file#*_}
done
for file in all_* ; do
    cp -pv "$file" /etc/opt/mythtv/${file#*_}
done
popd

chgrp mythtv /etc/opt/mythtv/mythtv.conf
cd install/opt/mythtv/bin
# Remove old script files
rm -f /opt/mythtv/bin/*
cp -pv `find . -maxdepth 1 -type f` /opt/mythtv/bin/
cp -pv $ver/* /opt/mythtv/bin/
chgrp mythtv /opt/mythtv/bin/*
cd $scriptpath/
if [[ "$IS_BACKEND" == true ]] ; then
    sudo cp install/etc/init/mythtv-backend.conf /etc/init/mythtv-backend.conf
fi

# Do we need to install monitor (Y or N)
if [[ "$USE_MONITOR" == Y ]] ; then
    sudo cp install/etc/init/mythtv-monitor.conf /etc/init/
fi
sudo mkdir -p /etc/lirc/
sudo cp install/etc/lirc/* /etc/lirc/
sudo mkdir -p /etc/rc_keymaps/
sudo cp install/etc/rc_keymaps/* /etc/rc_keymaps/
sudo cp install/etc/udev/rules.d/* /etc/udev/rules.d/
sudo cp install/etc/udev/rules.d/* /etc/udev/rules.d/
sudo rsync -rv --exclude=.svn install/usr/ /usr/
sudo cp install/etc/pm/sleep.d/* /etc/pm/sleep.d/
sudo cp install/etc/acpi/events/* /etc/acpi/events/

if ! grep '^catch22:' /etc/group ; then
    addgroup --gid 1099 catch22
fi
if ! grep '^catch22' /etc/passwd ; then
    sudo adduser --ingroup catch22 catch22
fi
if ! grep '^mythtv:.*catch22' /etc/group ; then
    sudo adduser catch22 mythtv
fi
sudo cp -a install/home/catch22/.mythtv /home/catch22/
sudo chown -R catch22 /home/catch22/.mythtv
sudo chgrp -R mythtv /home/catch22/.mythtv
sudo chmod -R g+w /home/catch22/.mythtv

# Remove automatic weekly db backup
if [[ -f /etc/cron.weekly/mythtv-database ]] ; then
    sudo mkdir -p /etc/cron.removed/
    sudo mv -f /etc/cron.weekly/mythtv-database /etc/cron.removed/
fi

